<template>
  <div class="my-services">
    <div class="page-header">
      <h1>我的服务</h1>
      <p class="subtitle">查看和管理您的服务预约</p>
    </div>

    <div class="services-container">
      <!-- 服务统计 -->
      <div class="stats-section">
        <div class="stat-card">
          <div class="stat-icon">
            <font-awesome-icon icon="calendar-check" />
          </div>
          <div class="stat-info">
            <span class="stat-value">{{ totalAppointments }}</span>
            <span class="stat-label">总预约数</span>
          </div>
        </div>
        <div class="stat-card">
          <div class="stat-icon">
            <font-awesome-icon icon="clock" />
          </div>
          <div class="stat-info">
            <span class="stat-value">{{ upcomingAppointments }}</span>
            <span class="stat-label">即将到来</span>
          </div>
        </div>
        <div class="stat-card">
          <div class="stat-icon">
            <font-awesome-icon icon="check-circle" />
          </div>
          <div class="stat-info">
            <span class="stat-value">{{ completedAppointments }}</span>
            <span class="stat-label">已完成</span>
          </div>
        </div>
      </div>

      <!-- 筛选标签 -->
      <div class="filter-tabs">
        <button 
          v-for="tab in filterTabs" 
          :key="tab.value"
          @click="activeFilter = tab.value"
          :class="['filter-tab', { active: activeFilter === tab.value }]"
        >
          {{ tab.label }}
        </button>
      </div>

      <!-- 服务列表 -->
      <div class="services-list">
        <div 
          v-for="appointment in filteredAppointments" 
          :key="appointment.id"
          class="service-card"
          :class="appointment.status"
        >
          <div class="service-header">
            <div class="service-info">
              <h3 class="service-name">{{ appointment.serviceName }}</h3>
              <span class="service-type">{{ appointment.serviceType }}</span>
            </div>
            <div class="service-status">
              <span :class="['status-badge', appointment.status]">
                {{ getStatusText(appointment.status) }}
              </span>
            </div>
          </div>
          
          <div class="service-details">
            <div class="detail-item">
              <font-awesome-icon icon="calendar" />
              <span>{{ appointment.date }}</span>
            </div>
            <div class="detail-item">
              <font-awesome-icon icon="clock" />
              <span>{{ appointment.time }}</span>
            </div>
            <div class="detail-item">
              <font-awesome-icon icon="map-marker-alt" />
              <span>{{ appointment.location || '上门服务' }}</span>
            </div>
          </div>

          <div class="service-actions">
            <button 
              v-if="appointment.status === 'upcoming'"
              @click="cancelAppointment(appointment.id)"
              class="action-btn cancel-btn"
            >
              取消预约
            </button>
            <button 
              v-if="appointment.status === 'upcoming'"
              @click="rescheduleAppointment(appointment)"
              class="action-btn reschedule-btn"
            >
              重新安排
            </button>
            <button 
              v-if="appointment.status === 'upcoming'"
              @click="markAsCompleted(appointment.id)"
              class="action-btn complete-btn"
            >
              标记完成
            </button>
            <button 
              v-if="appointment.status === 'completed'"
              @click="rateService(appointment)"
              class="action-btn rate-btn"
            >
              评价服务
            </button>
            <button 
              @click="viewDetails(appointment)"
              class="action-btn details-btn"
            >
              查看详情
            </button>
          </div>
        </div>

        <!-- 空状态 -->
        <div v-if="filteredAppointments.length === 0" class="empty-state">
          <font-awesome-icon icon="calendar-times" class="empty-icon" />
          <h3>暂无服务预约</h3>
          <p>{{ getEmptyStateText() }}</p>
          <router-link to="/service-appointment-and-matching" class="book-service-btn">
            立即预约服务
          </router-link>
        </div>
      </div>
    </div>

    <!-- 重新安排预约弹窗 -->
    <div v-if="showRescheduleDialog" class="dialog-overlay" @click="closeRescheduleDialog">
      <div class="dialog-content" @click.stop>
        <div class="dialog-header">
          <h2>重新安排预约</h2>
          <button class="close-btn" @click="closeRescheduleDialog">
            <font-awesome-icon icon="times" />
          </button>
        </div>
        <div class="dialog-body">
          <div class="form-group">
            <label>新预约日期</label>
            <input type="date" v-model="rescheduleData.date" class="form-input" :min="minDate">
          </div>
          <div class="form-group">
            <label>新预约时间</label>
            <input type="time" v-model="rescheduleData.time" class="form-input">
          </div>
        </div>
        <div class="dialog-footer">
          <button class="cancel-btn" @click="closeRescheduleDialog">取消</button>
          <button class="submit-btn" @click="confirmReschedule">确认重新安排</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'MyServices',
  data() {
    return {
      activeFilter: 'all',
      showRescheduleDialog: false,
      rescheduleData: {
        date: '',
        time: ''
      },
      currentAppointment: null,
      filterTabs: [
        { label: '全部', value: 'all' },
        { label: '即将到来', value: 'upcoming' },
        { label: '进行中', value: 'ongoing' },
        { label: '已完成', value: 'completed' },
        { label: '已取消', value: 'cancelled' }
      ],
      appointments: []
    }
  },
  mounted() {
    this.loadAppointments();
  },
  computed: {
    filteredAppointments() {
      if (this.activeFilter === 'all') {
        return this.appointments;
      }
      return this.appointments.filter(appointment => appointment.status === this.activeFilter);
    },
    totalAppointments() {
      return this.appointments.length;
    },
    upcomingAppointments() {
      return this.appointments.filter(appointment => appointment.status === 'upcoming').length;
    },
    completedAppointments() {
      return this.appointments.filter(appointment => appointment.status === 'completed').length;
    },
    minDate() {
      const today = new Date();
      return today.toISOString().split('T')[0];
    }
  },
  methods: {
    loadAppointments() {
      // 从localStorage加载预约数据
      const savedAppointments = JSON.parse(localStorage.getItem('appointments') || '[]');
      
      // 如果没有保存的预约数据，使用默认的示例数据
      if (savedAppointments.length === 0) {
        this.appointments = [
          {
            id: 1,
            serviceName: '家庭保洁',
            serviceType: '家政服务',
            date: '2024-06-15',
            time: '09:00',
            location: '上门服务',
            status: 'upcoming',
            duration: '3小时',
            requirements: '重点清洁厨房和卫生间'
          },
          {
            id: 2,
            serviceName: '营养套餐A',
            serviceType: '送餐服务',
            date: '2024-06-14',
            time: '12:00',
            location: '上门服务',
            status: 'completed',
            duration: '1次',
            requirements: '少盐少油'
          },
          {
            id: 3,
            serviceName: '上门护理',
            serviceType: '医疗健康',
            date: '2024-06-16',
            time: '14:00',
            location: '上门服务',
            status: 'upcoming',
            duration: '2小时',
            requirements: '血压监测和用药指导'
          },
          {
            id: 4,
            serviceName: '代购服务',
            serviceType: '家政服务',
            date: '2024-06-13',
            time: '10:00',
            location: '社区超市',
            status: 'completed',
            duration: '1次',
            requirements: '购买蔬菜、水果和日用品'
          }
        ];
      } else {
        this.appointments = savedAppointments;
      }
    },
    saveAppointments() {
      localStorage.setItem('appointments', JSON.stringify(this.appointments));
    },
    getStatusText(status) {
      const statusMap = {
        'upcoming': '即将到来',
        'ongoing': '进行中',
        'completed': '已完成',
        'cancelled': '已取消'
      };
      return statusMap[status] || status;
    },
    getEmptyStateText() {
      const textMap = {
        'all': '您还没有任何服务预约',
        'upcoming': '您没有即将到来的预约',
        'ongoing': '您没有正在进行的服务',
        'completed': '您还没有完成的服务记录',
        'cancelled': '您没有已取消的预约'
      };
      return textMap[this.activeFilter] || '暂无数据';
    },
    cancelAppointment(id) {
      if (confirm('确定要取消这个预约吗？')) {
        const appointment = this.appointments.find(a => a.id === id);
        if (appointment) {
          appointment.status = 'cancelled';
          this.saveAppointments();
          alert('预约已取消');
        }
      }
    },
    rescheduleAppointment(appointment) {
      this.currentAppointment = appointment;
      this.rescheduleData.date = appointment.date;
      this.rescheduleData.time = appointment.time;
      this.showRescheduleDialog = true;
    },
    closeRescheduleDialog() {
      this.showRescheduleDialog = false;
      this.currentAppointment = null;
      this.rescheduleData = { date: '', time: '' };
    },
    confirmReschedule() {
      if (!this.rescheduleData.date || !this.rescheduleData.time) {
        alert('请选择新的预约日期和时间');
        return;
      }
      
      if (this.currentAppointment) {
        this.currentAppointment.date = this.rescheduleData.date;
        this.currentAppointment.time = this.rescheduleData.time;
        this.saveAppointments();
        alert('预约时间已重新安排');
        this.closeRescheduleDialog();
      }
    },
    rateService(appointment) {
      alert(`为 ${appointment.serviceName} 服务进行评价`);
      // 这里可以打开评价弹窗
    },
    viewDetails(appointment) {
      alert(`查看 ${appointment.serviceName} 的详细信息`);
      // 这里可以打开详情弹窗
    },
    // 测试方法：将预约标记为已完成
    markAsCompleted(id) {
      const appointment = this.appointments.find(a => a.id === id);
      if (appointment) {
        appointment.status = 'completed';
        this.saveAppointments();
        alert('服务已完成');
      }
    }
  }
}
</script>

<style scoped>
.my-services {
  padding: 40px;
  background-color: #f8f9fa;
  min-height: 100vh;
}

.page-header {
  text-align: center;
  margin-bottom: 40px;
}

.page-header h1 {
  font-size: 32px;
  color: #2c3e50;
  margin-bottom: 10px;
}

.subtitle {
  color: #6c757d;
  font-size: 18px;
}

.services-container {
  max-width: 1200px;
  margin: 0 auto;
  display: grid;
  gap: 30px;
}

/* 统计卡片样式 */
.stats-section {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 20px;
}

.stat-card {
  background: white;
  border-radius: 16px;
  padding: 25px;
  display: flex;
  align-items: center;
  gap: 20px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
}

.stat-icon {
  width: 50px;
  height: 50px;
  background: #e3f2fd;
  border-radius: 25px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #1976d2;
  font-size: 20px;
}

.stat-info {
  display: flex;
  flex-direction: column;
}

.stat-value {
  font-size: 24px;
  font-weight: 600;
  color: #2c3e50;
}

.stat-label {
  color: #6c757d;
  font-size: 14px;
  margin-top: 5px;
}

/* 筛选标签样式 */
.filter-tabs {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.filter-tab {
  padding: 8px 16px;
  border: 2px solid #e0e0e0;
  background: white;
  border-radius: 20px;
  cursor: pointer;
  transition: all 0.3s ease;
  font-size: 14px;
}

.filter-tab:hover {
  border-color: #3498db;
  color: #3498db;
}

.filter-tab.active {
  background: #3498db;
  border-color: #3498db;
  color: white;
}

/* 服务卡片样式 */
.services-list {
  display: grid;
  gap: 20px;
}

.service-card {
  background: white;
  border-radius: 16px;
  padding: 25px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
  transition: transform 0.3s ease;
}

.service-card:hover {
  transform: translateY(-2px);
}

.service-card.upcoming {
  border-left: 4px solid #3498db;
}

.service-card.ongoing {
  border-left: 4px solid #f39c12;
}

.service-card.completed {
  border-left: 4px solid #27ae60;
}

.service-card.cancelled {
  border-left: 4px solid #e74c3c;
  opacity: 0.7;
}

.service-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 15px;
}

.service-info h3 {
  color: #2c3e50;
  font-size: 20px;
  margin-bottom: 5px;
}

.service-type {
  color: #6c757d;
  font-size: 14px;
  background: #f8f9fa;
  padding: 4px 8px;
  border-radius: 4px;
}

.status-badge {
  padding: 4px 12px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
}

.status-badge.upcoming {
  background: #e3f2fd;
  color: #1976d2;
}

.status-badge.ongoing {
  background: #fff3e0;
  color: #f57c00;
}

.status-badge.completed {
  background: #e8f5e8;
  color: #2e7d32;
}

.status-badge.cancelled {
  background: #ffebee;
  color: #c62828;
}

.service-details {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 15px;
  margin-bottom: 20px;
}

.detail-item {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #6c757d;
  font-size: 14px;
}

.service-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.action-btn {
  padding: 8px 16px;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s ease;
}

.cancel-btn {
  background: #e74c3c;
  color: white;
}

.cancel-btn:hover {
  background: #c0392b;
}

.reschedule-btn {
  background: #f39c12;
  color: white;
}

.reschedule-btn:hover {
  background: #e67e22;
}

.complete-btn {
  background: #27ae60;
  color: white;
}

.complete-btn:hover {
  background: #229954;
}

.rate-btn {
  background: #27ae60;
  color: white;
}

.rate-btn:hover {
  background: #229954;
}

.details-btn {
  background: #3498db;
  color: white;
}

.details-btn:hover {
  background: #2980b9;
}

/* 空状态样式 */
.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: #6c757d;
}

.empty-icon {
  font-size: 48px;
  margin-bottom: 20px;
  color: #dee2e6;
}

.empty-state h3 {
  color: #2c3e50;
  margin-bottom: 10px;
}

.book-service-btn {
  display: inline-block;
  margin-top: 20px;
  padding: 12px 24px;
  background: #3498db;
  color: white;
  text-decoration: none;
  border-radius: 8px;
  transition: background-color 0.3s ease;
}

.book-service-btn:hover {
  background: #2980b9;
}

/* 弹窗样式 */
.dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.dialog-content {
  background: white;
  border-radius: 16px;
  width: 90%;
  max-width: 500px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.dialog-header {
  padding: 20px;
  border-bottom: 1px solid #e0e0e0;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.dialog-header h2 {
  margin: 0;
  color: #2c3e50;
  font-size: 20px;
}

.close-btn {
  background: none;
  border: none;
  color: #666;
  cursor: pointer;
  font-size: 18px;
  padding: 5px;
}

.dialog-body {
  padding: 20px;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  color: #2c3e50;
  font-weight: 500;
}

.form-input {
  width: 100%;
  padding: 10px;
  border: 2px solid #e0e0e0;
  border-radius: 8px;
  font-size: 16px;
  transition: all 0.3s ease;
}

.form-input:focus {
  border-color: #3498db;
  outline: none;
  box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.1);
}

.dialog-footer {
  padding: 20px;
  border-top: 1px solid #e0e0e0;
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.cancel-btn, .submit-btn {
  padding: 10px 20px;
  border-radius: 8px;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.cancel-btn {
  background: white;
  border: 2px solid #e0e0e0;
  color: #666;
}

.submit-btn {
  background: #3498db;
  border: none;
  color: white;
}

.submit-btn:hover {
  background: #2980b9;
}

@media (max-width: 768px) {
  .my-services {
    padding: 20px;
  }

  .stats-section {
    grid-template-columns: 1fr;
  }

  .service-header {
    flex-direction: column;
    gap: 10px;
  }

  .service-details {
    grid-template-columns: 1fr;
  }

  .service-actions {
    justify-content: center;
  }

  .filter-tabs {
    justify-content: center;
  }
}
</style> 